package de.tum.activities.StatsApp;



import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.R.color;
import android.content.Context;
import android.graphics.Color;

public class AchievedPointsBarChart {
		 
	public GraphicalView execute(Context context, double[] archievedPoints, int maxPoints) {
		
		XYMultipleSeriesRenderer renderer = buildCategoryRenderer(maxPoints);
		 

		XYMultipleSeriesDataset categorySeries = new XYMultipleSeriesDataset();
		CategorySeries archievedPointsSeries = new CategorySeries("Occurence of archieved points");
		for(double i=0; i<=maxPoints; i=i+0.5){
			archievedPointsSeries.add(""+i, getOccurencesOfSimplePoints(archievedPoints, i));
		}
		
		categorySeries.addSeries(archievedPointsSeries.toXYSeries());
		
		return ChartFactory.getBarChartView(context, categorySeries, renderer, Type.DEFAULT);
	}
		 
	protected XYMultipleSeriesRenderer buildCategoryRenderer(int maxPoints) {
		XYMultipleSeriesRenderer XYMultipleRenderer = new XYMultipleSeriesRenderer();
		XYSeriesRenderer XYRenderer = new XYSeriesRenderer();

		XYRenderer.setColor(Color.BLUE);
	    XYRenderer.setPointStyle(PointStyle.SQUARE);
	    XYRenderer.setFillBelowLine(true);
	    XYRenderer.setFillBelowLineColor(Color.WHITE);
	    XYRenderer.setFillPoints(true);
	    
	    
	    XYMultipleRenderer.setBarSpacing(0.5);
	    XYMultipleRenderer.setXLabels(0);
	    for(double i=0, j=1; i<=maxPoints; i=i+0.5, j++){
	    	
	    	if(i - ((int) i) == 0.0)
	    	 XYMultipleRenderer.addXTextLabel(j, ""+(int)i);
		}
	    
	    XYMultipleRenderer.addSeriesRenderer(XYRenderer);
	  	    
		return XYMultipleRenderer;
	}
	
	
	private int getOccurencesOfSimplePoints(double[] archievedPoints, double currentPoint){
		int counter = 0;
		for(int i=0; i<archievedPoints.length; i++){
			if(archievedPoints[i]== currentPoint)
				counter++;
		}
		
		return counter;
	}
}